import pygame
from pygame.locals import *

from lib.Loader import Loader

class MenuPart(object):
    def __init__(self):
        self.loader = Loader()
        self.back = self.loader.load_image("logo.png").convert()
        self.frame = self.loader.load_image("picframe.png").convert_alpha()
        self.fnt = self.loader.load_font("FreeSans.ttf",30)
        self.fnt2 = self.loader.load_font("FreeSans.ttf",18)

        #self.txt = self.fnt.render("Select motive:", True, (255,255,0))
        self.txtr = self.txt.get_rect()
        self.txtr.center = (400,365)

        self.next_part = 0

        #self._make_motives()
        #if len(self.motives):
        #    self.motive_r = self.motives[0][0].get_rect()
        #    self.motive_r.center = (400,475)

        #self.motive_idx = 0

        #self.left = self.loader.load_image("arrow_left.png").convert_alpha()
        #self.left_r = self.left.get_rect()
        #self.left_r.center = (400-150,475)

        #self.right = self.loader.load_image("arrow_right.png").convert_alpha()
        #self.right_r = self.right.get_rect()
        #self.right_r.center = (400+150,475)
    
    def fix_text(self,txt):
        return " ".join(txt.split(".")[0].split("_"))

    def _make_motives(self):
        l = self.loader.list_motives()        
        print len(l)
        self.motives = []
        for f in l:
            try:
                img = self.loader.load_motive(f)
                print f
            #    r = i.get_rect()
            #   print "r.w = " + r.w
#                print "r.h = " + r.h
#                print "cargando recta"n 
#                if r.w != 640 or r.h != 512:
#                    print "en if"
#                    continue
                m = self.frame.copy()                
#                img = pygame.transform.smoothscale(img,(300, 250))
                #m.blit(img,(17,17))               
                self.motives.append((m,f, self.fnt2.render(self.fix_text(f), True, (255,255,0))))                
            except:
                pass

    def event(self,event):
        if event.type == KEYDOWN:
            if event.key == K_ESCAPE:
                self.next_part = -1
        elif event.type == MOUSEBUTTONDOWN:
            if self.left_r.collidepoint(event.pos):
                self.motive_idx-=1
                if self.motive_idx < 0:
                    self.motive_idx=len(self.motives)-1
            elif self.right_r.collidepoint(event.pos):
                self.motive_idx+=1
                if self.motive_idx==len(self.motives):
                    self.motive_idx=0
            elif self.motive_r.collidepoint(event.pos):
                self.next_part = 1
        elif event.type == MOUSEBUTTONUP:
            pass
        elif event.type == MOUSEMOTION:
            pass

    def get_motive(self):
        return self.motives[self.motive_idx][1]

    def update(self,screen):
        #screen.blit(self.back,(0,0))

        if not len(self.motives):
            print "Error: No motives found."
            return -1

        m = self.motives[self.motive_idx][0]
        #screen.blit(m,self.motive_r.topleft)        
        t = self.motives[self.motive_idx][2]
        tr = t.get_rect()
        tr.center = (400,577)

        #screen.blit(self.left,self.left_r.topleft)
        #screen.blit(self.right,self.right_r.topleft)
            
        #screen.blit(self.txt,self.txtr.topleft)        
        #screen.blit(t,tr.topleft)        
        return self.next_part

